﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OneRed.Data
{
    public class FormatData : DataBase
    {
        private string psJournal;
        public string sJournal
        {
            get
            {
                return psJournal;
            }
            set
            {
                psJournal = value;
                OnPropertyChanged("sJournal");
            }
        }//JF 信息工程大学学报

        private string psCites;//引文
        public string sCites
        {
            get
            {
                return psCites;
            }
            set
            {
                psCites = value;
                OnPropertyChanged("sCites");
            }
        }
        private string psCLCode;//分类号
        public string sCLCode
        {
            get
            {
                return psCLCode;
            }
            set
            {
                psCLCode = value;
                OnPropertyChanged("sCLCode");
            }
        }

        new public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    public class FormatDataCollection
    {
        private static ObservableCollection<FormatData> formatDatas = new();

        //获取数据集
        public static ObservableCollection<FormatData> Results()
        {
            return formatDatas;
        }
        //获取被选中的数据
        public static int SelectedCount()
        {
            int iretun = 0;
            if (formatDatas == null)
                iretun = 0;
            iretun = formatDatas.Where(t => t.IsChecked).Count();
            return iretun;
        }
        public static int Count()
        {
            if (formatDatas != null)
                return formatDatas.Count();
            else
                return 0;
        }
        //清空数据
        public static bool Clear()
        {
            if (formatDatas != null)
                formatDatas.Clear();
            return true;
        }
        //向数据集中增加单条数据
        public static bool addData(FormatData InputData)
        {
            try
            {
                formatDatas.Add(InputData);
                return true;
            }
            catch
            {
                return false;
            }
        }


        //向数据集中增加数组 blClearOriData是否清空原有的数据，默认为false
        public static bool addData(List<FormatData> InputDatas, Boolean blClearOriData = false)
        {
            try
            {
                if (blClearOriData)
                    Clear();

                foreach (FormatData fd in InputDatas)
                {
                    formatDatas.Add(fd);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        //基于ID查找数据
        public static FormatData FindData(string id)
        {
            return formatDatas.First(d => d.gID == id);
        }

        //基于ID删除数据
        public static bool DeleteData(string id)
        {
            try
            {
                return formatDatas.Remove(FindData(id));
            }
            catch
            {
                return false;
            }
        }

        //删除被选中的数据
        public static void DeleteSelectedData()
        {
            foreach (var fd in formatDatas.Where(t => t.IsChecked).ToList())
                formatDatas.Remove(fd);
        }
    }
}